﻿using log4net;
using log4net.Config;
using log4net.Repository;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace Stall.Service.Common
{
    public class LogTool
    {
        #region Log
        private static ILoggerRepository repository { get; set; }
        private static ILog Logger { get; set; }
        private static ILogger printLogger;
        private static readonly object obj = new object();

        public static void InitLog(ILogger _logger)
        {
            try
            {
                repository = LogManager.CreateRepository("NETCoreRepository");
                string assemblyFilePath = Assembly.GetExecutingAssembly().Location;
                string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath);
                string configFilePath = assemblyDirPath + "/log4net.config";

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    configFilePath = assemblyDirPath + "/log4net_linux.config";
                }

                XmlConfigurator.ConfigureAndWatch(repository, new FileInfo(configFilePath));

                Logger = LogManager.GetLogger(repository.Name, typeof(LogTool));
                printLogger = _logger;

                LogTool.Info("初始化Log成功");
            }
            catch (Exception e)
            {
                LogTool.Error("初始化Log失败");
            }
        }

        public static void Info(string info)
        {

            if (Logger != null)
                Logger.Info(info);

            if (printLogger != null)
                printLogger.LogInformation(info);
        }

        public static void Warn(string warn)
        {
            if (Logger != null)
                Logger.Warn(warn);

            if (printLogger != null)
                printLogger.LogWarning(warn);
        }

        public static void Error(string error)
        {

            if (Logger != null)
                Logger.Error(error);

            if (printLogger != null)
                printLogger.LogError(error);
        }



        #endregion
    }
}
